<?php

    class MessageModel {
        private $conn;

//        连接数据库
        public function __construct() {
            $this->conn = new mysqli('localhost', 'root', 'root123','oopdb');
            if($this->conn->connect_error){
                die('数据库连接失败！'.$this->conn->connect_error);
            }
        }

//        插入留言
        public function addMessage($name,$message){
            $sql = "insert into message (name,content) values('$name','$message')";
            return $this->conn->query($sql);

        }

//        计算留言总数
        public function countMessage(){
            $sql = "select count(*) as total from message";
            $result = $this->conn->query($sql);
            $row = $result->fetch_assoc();
            return $row['total'];
        }

//        根据页数计算出的偏移量拿到五条留言，默认从0开始
        public function getMessage($start=0,$limit){


            $sql = "select * from message order by id desc limit $start,$limit ";
            $result = $this->conn->query($sql);
            $message = [];
            while($row = $result->fetch_assoc()){
                $message[] = $row;
            }

            return $message;
        }

    }


?>